package com.erpoa.controller;

import com.alibaba.fastjson.JSON;
import com.erpoa.dto.DealMessage;
import com.erpoa.dto.ResultBean;
import com.erpoa.entity.PackMessageByWuLiu;
import com.erpoa.service.CarDataService;
import com.erpoa.service.SessionManager;
import com.erpoa.utils.PDAException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;


/**
 * @description:
 * @author: jamin.chen
 * @createDate: 2020/1/3 9:37
 * @version: 1.0
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "WuLiuController", tags = {"物流中心pda接口"})
@RestController
@RequestMapping("/PDAService/wuliu")
public class WuLiuController {
    private static final Logger logger = LoggerFactory.getLogger(WuLiuController.class);

    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private CarDataService carDataService;


    @ApiOperation("物流跑批5分钟内的数据导入redis")
    @RequestMapping(value = "wuliuRedis", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    //  @NoRepeatSubmit(key = "sendMail:arg[0]")
    public String wuliuRedis(@RequestParam(value = "id", required = true) String id) {
        carDataService.wuliuRedis(id);
        return "HJ";
    }


    @ApiOperation("物流补偿根据redis中detail托盘号")
    @RequestMapping(value = "compensateTrayNo", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    //  @NoRepeatSubmit(key = "sendMail:arg[0]")
    public String compensateTrayNo(@RequestParam(value = "trayNumber", required = true) String trayNumber) {

        return carDataService.compensateTrayNo(trayNumber);

    }





    /**
     * wuLiuFahuoServlet
     * @return
     * @throws Exception
     */
    @ApiOperation("物流复核pda综合接口")
    @RequestMapping(value = {"/wuLiuFahuoServlet"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DealMessage wuLiuFahuoServlet(@RequestBody PackMessageByWuLiu packMessage)   {
            DealMessage dealMessage = new DealMessage(true);
        int optType = packMessage.getOptType();

        try {
            logger.debug("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());
            switch (optType) {
                //第一次查询物流条码 获取客户，DS,出库单号，波次等等
                case 0:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.checkWuLiuFaHuoByBarcodeFirst(packMessage)));
                   break;
                //batchListServlet根据客户信息查询发货信息
              case 1:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.requestDeliveryDetail(packMessage)));
                    break;
                //当前托盘二次扫描及后续扫描
                  case 2:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.checkWuLiuFaHuoByBarcode(packMessage)));
                    break;
                //托盘确认
                case 4:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.confirmTray(packMessage)));
                    break;
                //托盘删除
                case 5:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.delTray(packMessage)));
                    break;
                //验证外箱条码
                case 6:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.checkPckBarCode(packMessage)));
                    break;
                //零散箱自动新增托盘
                case 7:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.antoCreateWithSurplus(packMessage)));
                    break;
                //手动拼箱
               case 8:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.getComfirmLcl(packMessage)));
                    break;
                //提交并保存手动拼箱
                 case 9:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.submitComfirmLcl(packMessage)));
                    break;
                //更新数据
               case 10:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.updateDataByCustName(packMessage)));
                    break;
                //手工确认sku
                  case 11:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.pckConfirm(packMessage)));
                    break;
                //获取当前客户未打印的数据
                case 12:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.trayConfirm(packMessage)));
                    break;
                //获取未复核数据
                case 14:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.descGet(packMessage)));
                    break;
                //获取客户数据
                case 15:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.selectSname(packMessage)));
                    break;
                //获取托盘数据
                case 16:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.selectTray(packMessage)));
                    break;
                //检查是否复合过
                case 17:
                    carDataService.checkWuliuBarCodeFlag(packMessage);
                    break;
                //预出波次汇总数量
                case 18:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.countAllDescBySname(packMessage)));
                    break;
                //查询已扫描和托盘已扫描
                case 19:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.counthasscanDEtail(packMessage)));
                    break;
                //查询托盘已扫描记录
                case 20:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.selectTrayDetailByTrayNo(packMessage)));
                    break;
                //生成托盘号
                case 21:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.createTrayNumber(packMessage)));
                    break;
                //汇总数量
                case 22:
                    dealMessage.setMessage(JSON.toJSONString(carDataService.huizongcustName(packMessage)));
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof PDAException) {
                dealMessage = new DealMessage(false, e.getMessage());
            } else if (e instanceof DuplicateKeyException) {
                throw new PDAException("数据重复，请重新扫描！");

            } else if (e instanceof SQLException) {
                throw new PDAException("数据重复，请重新扫描！");
            }
            else {
                dealMessage = new DealMessage(false, "数据处理出现未知错误，请再扫一次确认结果，如多次出现请及时反馈IT，分机号：6257！时间点：" + System.currentTimeMillis() + "错误信息：" + e.getMessage());
            }
            logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            return dealMessage;
        }
        logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        return dealMessage;
    }

    /**
     * 登入创建session
     * @return
     * @throws Exception
     */
    @Deprecated
    @ApiOperation("登入创建session废弃")
    @RequestMapping(value = {"/login"}, method = RequestMethod.POST)
    public ResultBean login(@RequestParam String username, @RequestParam String password) throws Exception {
        logger.debug("username = " + username);
        logger.debug("password:" + DigestUtils.md5Hex(password));
        return sessionManager.login(username, password);
    }

}
